package org.openmrs.module.basicmodule.DSS.compiler;

import org.openmrs.module.basicmodule.DSS.visitor.PrintVisitor;
import org.openmrs.module.basicmodule.DSS.ast.AST;
import org.openmrs.module.basicmodule.DSS.Interpreter.InterpreterVisitor;
import java.io.File;
import java.util.Properties;
import org.openmrs.api.context.Context;
import org.openmrs.module.basicmodule.DSS.XMLApp.XMLGenerator;
import org.openmrs.util.DatabaseUpdateException;
import org.openmrs.util.InputRequiredException;
import org.openmrs.util.OpenmrsUtil;
import org.openmrs.module.basicmodule.DSS.parser.Parser;

/**
 *  The Compiler class contains the main program for compiling
 *  a source program to bytecodes
*/
public class Compiler {

/**
 * The Compiler class reads and compiles a source program
*/
        int patientId;	
	String sourceFile;
//     private static Properties props;	
    public Compiler(String sourceFile) {
    	this.sourceFile = sourceFile;
        this.patientId=3;
    }

    public Compiler(String sourceFile, int patientId) {
	        this.sourceFile = sourceFile;
	        this.patientId = patientId;
    }
  
//    public void start() throws DatabaseUpdateException, InputRequiredException
//    {
//        System.out.flush();
//    String dir="C:\\Users\\Akhil\\AppData\\Roaming\\OpenMRS";
////     File propsFile = new File(dir, "openmrs-runtime.properties");
//    props = new Properties();
//    OpenmrsUtil.loadProperties(props, propsFile); 
//    
//    Context.startup("jdbc:mysql://localhost:3306/openmrs?autoReconnect=true", "user", "user123", props);
//  
//    Context.openSession();
//    Context.authenticate("admin", "Admin123");    
//    }
//  public void shutdown()
//  {
//     Context.closeSession();
//  }
    
    public String compileProgram() {
        try {
//            start();
            Parser parser = new Parser(sourceFile);
            AST t = parser.execute();
            PrintVisitor pv = new PrintVisitor();
            t.accept(pv);
            XMLGenerator xg = new XMLGenerator("TestXML.xml");
            //XMLParser xp =new XMLParser();
            //xp.ReadX();
            System.out.println("\n\n--------------\n\n");
            InterpreterVisitor iv = new InterpreterVisitor(this.patientId);
            t.accept(iv);
//            shutdown();
            return "DSS Rule Compiled Successfully";
        }catch (Exception e) {
            System.out.println("********exception*******"+e.toString());
         };
         return "Problem";
    }
    
//    public static void main(String args[]) {
////        if (args.length == 0) {
////            System.out.println("***Incorrect usage, try: java compiler.Compiler <file>");
////            System.exit(1);
////        }
//        new Compiler("sample.dss").compileProgram();
//    }
}
